﻿using AppSocketModel;
using AppSocketService.Account;
using BCGPSCommon;
using BCGPSMongoData;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;

namespace AppSocketService.Company
{
    public class CompanyService :ServiceBase, ICompanyService
    {
        private MongoDBCommand<CompanyDto>  mongoDBCommand = new MongoDBCommand<CompanyDto>("Company"); 
        private readonly SystemAccountDto systemAccount;
        public CompanyService(IClaimsAccessor claimsAccesso, ISystemAccountService systemAccountService) : base(claimsAccesso)
        {
            systemAccount = systemAccountService.GetCurrentAccount();
        }
        public CompanyService()
        {

        }
        public CompanyDto AddNew(CompanyRequestDto  companyRequestDto )
        {
            var vid = mongoDBCommand.SelectDocumentAsync(s => s.CompanyId == companyRequestDto.CompanyId).GetAwaiter().GetResult() ;
            if (vid != null)
                throw new ArgumentException($"ID[{companyRequestDto.CompanyId}]已被占用!");

            var a = mongoDBCommand.SelectDocumentAsync(s => s.CompanyName == companyRequestDto.CompanyName).GetAwaiter().GetResult();
            if (a != null)
                throw new ArgumentException("公司名称已存在");

            var dto = companyRequestDto.As<CompanyDto>();
            dto.CreateTime = BCGPSCommon.MongoDBDateTime.GetNowUtc();
            dto.CreateAccountCode = systemAccount.SystemAccountCode;
            dto.ModifyTime = BCGPSCommon.MongoDBDateTime.GetNowUtc();
            dto.ModifyAccountCode = systemAccount.SystemAccountCode;
            mongoDBCommand.InsertDocumentAsync(dto);
            return dto;
        }

        public CompanyDto Update(CompanyDto dto )
        {
            var update = Builders<CompanyDto>.Update
                .Set(s=>s.ModifyAccountCode ,systemAccount.SystemAccountCode)
                .Set(s=>s.ModifyTime ,MongoDBDateTime.GetNowUtc())
                .Set(s=>s.AccountRegisterCode,dto.AccountRegisterCode)
                .Set(s=>s.IsRelay,dto.IsRelay)
                .Set(s => s.CompanyName, dto.CompanyName);

            return  mongoDBCommand.UpdateDocumentAsync(s => s.CompanyId == dto.CompanyId, update).GetAwaiter().GetResult();
        }
        public CompanyDto Get(int companyId)
        {
            return mongoDBCommand.SelectDocumentAsync(s => s.CompanyId == companyId).GetAwaiter().GetResult();
        }
        public CompanyDto GetCompany (string AccountRegisterCode, string companyName)
        {
            return mongoDBCommand.SelectDocumentAsync(s =>s.AccountRegisterCode == AccountRegisterCode && s.CompanyName == companyName).GetAwaiter().GetResult();
        }

        public PageableList<CompanyDto> GetList(string companyName, bool desc,int index,int size)
        {
            index = index > 0 ? index - 1 : 0; 
            if (!string.IsNullOrWhiteSpace(companyName))
            {
                return mongoDBCommand.SelectDocumentsAsync(s => s.CompanyName.Contains(companyName), s => s.CompanyId, desc, index*size, size).GetAwaiter().GetResult();
            }
            return mongoDBCommand.SelectDocumentsAsync(s => s.CompanyName!=string.Empty, s => s.CompanyId, desc, index*size, size).GetAwaiter().GetResult();
        }

        public bool Delete(int companyId)
        {
            IAccountService accountService = new AccountService(new RedisService());

            if (accountService.GetCompanyAccountCount(companyId) > 0)
                throw new ArgumentException("不能删除已激活的公司!");

            if (mongoDBCommand.DeleteDocumentAsync(s => s.CompanyId == companyId).GetAwaiter().GetResult().DeletedCount > 0)
                return true;
            else
                throw new Exception("没有数据被删除!");
        }
    }
}
